package com.gzhu.knowledgeAdmin.controller;

import com.gzhu.knowledgeAdmin.common.param.TaskParam;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.valid.AddGroup;
import com.gzhu.knowledgeAdmin.common.valid.UpdateGroup;
import com.gzhu.knowledgeAdmin.common.vo.PageVo;
import com.gzhu.knowledgeAdmin.common.vo.RuleDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.gzhu.knowledgeAdmin.entity.Rule;
import com.gzhu.knowledgeAdmin.service.RuleService;

import java.io.IOException;
import java.util.Map;
import java.util.List;

/**
 * 规则表
 *
 * @author yangminggui
 * @email giaogiao@gmail.com
 * @date 2024-01-25 23:16:07
 */
@RestController
@RequestMapping("knowledgeAdmin/rule")
public class RuleController {
    @Autowired
    private RuleService ruleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageVo page = ruleService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        RuleDetailVo rule = ruleService.getRuleById(id);
        return R.ok(rule);
    }

    /**
     * 添加
     */
    @PostMapping("/save")
    public R save(@RequestBody @Validated(AddGroup.class) Rule rule) {
        ruleService.saveRule(rule);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody @Validated(UpdateGroup.class) Rule rule) {
        ruleService.updateRuleById(rule);
        return R.ok();
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody List<Integer> ids) {
        ruleService.removeRulesByIds(ids);
        return R.ok();
    }

    /**
     * 规则检测
     */
    @PostMapping("/check")
    public R save(@RequestBody TaskParam taskParam) throws Exception {
        ruleService.checkBatch(taskParam);
        return R.ok();
    }

}
